package com.template.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.modelmapper.ModelMapper;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ConfigBean {
    //配置springMVC返回的json 是使用 fastjson
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        //下面是配置一些 fastjson信息 例如：SerializerFeature.WriteMapNullValue 让值 为null 的属性也在 json中显示
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,
                SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.WriteMapNullValue);
        fastConverter.setFastJsonConfig(fastJsonConfig);
        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }

    /**
     * @Author xjw
     * @Date 2019/1/18 15:11
     * @Description :  用于 发起 http请求
     **/
    @Bean
    public RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory resquestFactory = new SimpleClientHttpRequestFactory();
        resquestFactory.setConnectTimeout(15000);
        resquestFactory.setReadTimeout(15000);
        return new RestTemplate(resquestFactory);
    }

    @Bean
    public ModelMapper modelMapper() {
        return new ModelMapper();
    }

}
